<?php

/**
 * partners actions.
 *
 * @package    sf_sandbox
 * @subpackage partners
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class partnersActions extends autopartnersActions
{
    
    public function executeIndex()
    {
        $c = new Criteria();
		$c->addAscendingOrderByColumn(PartnerPeer::GEO_NAME_ID);
        $this->partners = PartnerPeer::doSelect($c);
    }
    
    public function executeGetlist()
    {
        $mask = trim(strip_tags($this->getRequestParameter('mask', '')));
        $len = strlen($mask);
        
        $c = new Criteria();
        $c->add(GeoNamePeer::COUNTRY_CODE, 'LT');
        if( $len > 3 )
        {
            $c->add(GeoNamePeer::ASCII_NAME, "%" . $mask . "%", Criteria::LIKE);
            $c->addDescendingOrderByColumn(GeoNamePeer::POPULATION);
        }else 
        {
            $c->add(GeoNamePeer::ASCII_NAME, $mask . "%", Criteria::LIKE);
            $c->addDescendingOrderByColumn(GeoNamePeer::PRIORITY);
        }

        $this->geos = GeoNamePeer::doSelect($c);
    }
    
    protected function updatePartnerFromRequest()
    {
        parent::updatePartnerFromRequest();
        
//        debugvar('Photo: ' . $this->getRequest()->getFileName('photo'));
        if( !$this->getRequest()->hasErrors() && $this->getRequest()->getFileSize('photo') )
        {
            $filename = md5($this->partner->getId()) . $this->getRequest()->getFileExtension('photo');
            $this->getRequest()->moveFile('photo', sfConfig::get('sf_upload_dir') . '/partners/' . $filename);
            $this->partner->setFilePath($filename);
//            debugvar($this->partner);
        }
//        exit('updated from request');
    }
    
    /**
     * Deletes partner from DB and its logo
     *
     * @param Partners $partners
     */
    protected function deletePartner($partner)
    {
        $filename = sfConfig::get('sf_upload_dir') . '/partners/' . $partner->getFilePath();
//        debugvar('Photo to delete: ' . $filename);
        if( is_file($filename) )
        {
            @unlink($filename);
        }

        $partner->delete();
    }
    
}
